package com.example.springsecurity.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.springsecurity.annotation.Anonymous;
import com.example.springsecurity.common.CommonResult;
import com.example.springsecurity.security.AuthenticationThreadLocal;
import com.example.springsecurity.security.dto.LoginUserDto;
import com.example.springsecurity.serivce.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author wuancheng
 * @date 2024/6/19 17:19
 * @description
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private AuthenticationManager authenticationManager;

    @Resource
    private TokenService tokenService;

    /**
     * 获取验证码
     *
     * @return
     */
    @Anonymous
    @PostMapping("/getCode")
    public String getCode(){
        return "wac";
    }

    /**
     * 用户登录
     *
     * @param jsonObject
     * @return
     */
    @Anonymous
    @PostMapping("/login")
    public CommonResult login(@RequestBody JSONObject jsonObject) {
        Authentication authentication;
        try {
            String userName = jsonObject.getString("userName");
            String password = jsonObject.getString("password");
            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userName, password);
            // 保存登录的令牌信息在当前线程的threadLocal中
            AuthenticationThreadLocal.setContext(authenticationToken);
            // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
            authentication = authenticationManager.authenticate(authenticationToken);
        } catch (Exception e) {
            throw new RuntimeException();
        } finally {
            AuthenticationThreadLocal.clearContext();
        }
        // 做登录成功后保存或更新用户登录信息或登录日志的处理
        LoginUserDto userDto = (LoginUserDto) authentication.getPrincipal();
        String token = tokenService.generateToken(userDto);
        JSONObject result = new JSONObject();
        result.put("user", userDto);
        result.put("token", token);
        return CommonResult.succeed(result);
    }

    /**
     * 获取个人信息
     *
     * @return
     */
    @PreAuthorize("@permissionService.hasPermission('user:info:m')")
    @PostMapping("/getMySelf")
    public CommonResult getMySelf(){
        return CommonResult.succeed("mySelf");
    }

    /**
     * 用户退出
     *
     * @param jsonObject
     * @return
     */
    @Anonymous
    @PostMapping("/logout")
    public CommonResult logout(@RequestBody JSONObject jsonObject){
        return CommonResult.succeed();
    }
}
